var s_main_id = getUrlParam("main_id");
var datas = {};
var index_array = [];

//初始化
function biz_start() {
	$("#datetime").text(new Date( +new Date() + 8 * 3600 * 1000 ).toJSON().substr(0,19).replace("T"," "));
	var data_param = {"param_name": "A01_DOCHELP","session_id": session_id,"param_value1": s_main_id};
	get_ajax_baseurl(data_param,"A01_DOCHELP");
}

function A01_DOCHELP(input) {
	datas = input;
	let t_proc_name_list = datas.A01_DOCHELP.t_proc_name;
	$.each(t_proc_name_list,function(i, obj) {
		let main_id = obj.MAIN_ID;
		let index_string = index_array.toString();
		if(index_array.indexOf(main_id) == -1){
			index_array.push(main_id);
		}
	});
	get_t_proc_name_list();
	get_t_proc_each();
	layer.close(ly_index);
}

//循环获取各个接口
function get_t_proc_each(){
	let objTag = $("[name='T_INTERFACE']");
	let $div = $("[name='T_INTERFACE']");
	for(let i=0;i<index_array.length;i++){
		let NewO = document.createElement("div");
		$(NewO).html("<div name=\"T_INTERFACE_"+index_array[i]+"\">"+objTag.html()+"</div>");
		$div.before(NewO);
		get_t_proc_name_detail(i,index_array[i]);
		get_t_proc_inparam(i,index_array[i]);
		get_t_proc_outparam(i,index_array[i]);
		get_t_proc_return(i,index_array[i]);
	}
	$("[name='T_INTERFACE']").remove();
}

//接口列表
function get_t_proc_name_list(){
	let msg = datas.A01_DOCHELP.t_proc_name;
	if($("[name='T_PROC_NAME_LIST']").length>0){ 
		let objTag = $("[name='T_PROC_NAME_LIST']");
		$.each(msg,function(i, obj) {
			let objTagNew = base_child_table(obj,"T_PROC_NAME_",objTag,i);
			let row_index = $(objTag)[0].rowIndex;
			let $tr = $(objTag).parent().find("tr").eq(row_index);
			$tr.before(objTagNew);
		});
		$(objTag).remove();
	}
}

//接口明细
function get_t_proc_name_detail(index,proc_id){
	let t_proc_name = datas.A01_DOCHELP.t_proc_name[index];
	let t_sub_userpower_all = datas.A01_DOCHELP.t_sub_userpower;
	let t_proc_inparam_all = datas.A01_DOCHELP.t_proc_inparam;
	let msg = t_proc_name;
	let interface_address = baseUrl + "&param_name=" + msg.INF_EN_NAME;
	let main_id = t_proc_name.MAIN_ID;
	let inparam_count = 0;
	$.each(t_proc_inparam_all,function(i, obj) {
		if(obj.PROC_ID == main_id){
			inparam_count = inparam_count + 1;
		}
	});
	let interface_example = interface_address;
	for(let i=0;i<inparam_count;i++){
		interface_example = interface_example+"&param_value"+(i+1)+"=";
	}
	msg.INTERFACE_ADDRESS = interface_address;
	msg.INTERFACE_EXAMPLE = interface_example;
	$.each(t_sub_userpower_all,function(i, obj) {
		if(obj.PROC_ID == main_id){
			msg.LIMIT_DATE = obj.LIMIT_DATE;
			msg.LIMIT_TAG = obj.LIMIT_TAG;
			msg.LIMIT_NUMBER = obj.LIMIT_NUMBER;
			msg.USE_LIMIT = obj.USE_LIMIT;
		}
	});
	base_table("T_PROC_NAME_DETAIL_",msg,index,proc_id);
}

//输入参数
function get_t_proc_inparam(index,proc_id){
	let t_proc_inparam = datas.A01_DOCHELP.t_proc_inparam;
	let msg = [];
	$.each(t_proc_inparam,function(i, obj) {
		if(obj.PROC_ID == proc_id){
			msg.push(obj);
		}
	});
	if($("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='T_PROC_INPARAM']").length>0){
		let objTag = $("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='T_PROC_INPARAM']");
		$.each(msg,function(i, obj) {
			let objTagNew = base_child_table(obj,"T_PROC_INPARAM_",objTag,i);
			let row_index = $(objTag)[0].rowIndex;
			let $tr = $(objTag).parent().find("tr").eq(row_index);
			$tr.before(objTagNew);
		});
		$(objTag).remove();
	}
}

//输出参数
function get_t_proc_outparam(index,proc_id){
	let t_proc_outparam = datas.A01_DOCHELP.t_proc_outparam;
	let msg = [];
	$.each(t_proc_outparam,function(i, obj) {
		if(obj.PROC_ID == proc_id){
			msg.push(obj);
		}
	});
	if($("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='T_PROC_OUTPARAM']").length>0){
		let objTag = $("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='T_PROC_OUTPARAM']");
		$.each(msg,function(i, obj) {
			let objTagNew = base_child_table(obj,"T_PROC_OUTPARAM_",objTag,i);
			let row_index = $(objTag)[0].rowIndex;
			let $tr = $(objTag).parent().find("tr").eq(row_index);
			$tr.before(objTagNew);
		});
		$(objTag).remove();
	}
}

//返回值
function get_t_proc_return(index,proc_id){
	let t_proc_return = datas.A01_DOCHELP.t_proc_return;
	let msg = [];
	$.each(t_proc_return,function(i, obj) {
		if(obj.PROC_ID == proc_id){
			msg.push(obj);
		}
	});
	if($("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='T_PROC_RETURN']").length>0){
		let objTag = $("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='T_PROC_RETURN']");
		$.each(msg,function(i, obj) {
			let objTagNew = base_child_table(obj,"T_PROC_RETURN_",objTag,i);
			let row_index = $(objTag)[0].rowIndex;
			let $tr = $(objTag).parent().find("tr").eq(row_index);
			$tr.before(objTagNew);
		});
		$(objTag).remove();
	}
}

//当前元素数据填充
function base_child_table(msg,dt_name,objTag,i){
	let NewO = document.createElement("tr")
	$(NewO).html(objTag.html());
	for(let obj in msg)
	{
		let o_text = s_decode(msg[obj]);  
		if(dt_name == "T_PROC_NAME_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(i+1));
		}
		else if(dt_name == "T_PROC_INPARAM_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(i+1));
		}
		else if(dt_name == "T_PROC_OUTPARAM_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(i+1));
		}
		else if(dt_name == "T_PROC_RETURN_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(i+1));
		}
		$(NewO).find("[name='"+dt_name+obj+"']").find("span").text(o_text);
	}
	return NewO;
}

//模板元素数据填充
function base_temp_table(msg,dt_name){
	for(let obj in msg)
	{
		let o_text = s_decode(msg[obj]);
		$("[name='"+dt_name+obj+"']").find("span").text(o_text);
	}
}

//基本表格数据填充
function base_table(dt_name,msg){
	for(let obj in msg)
	{
		let o_text = s_decode(msg[obj]);
		$("[name='"+dt_name+obj+"']").find("span").text(o_text);
	}
}

//循环基本表格数据填充
function base_table(dt_name,msg,index,proc_id){
	for(let obj in msg)
	{
		let o_text = s_decode(msg[obj]);
		if(dt_name == "T_PROC_NAME_DETAIL_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(index+1)+".");
		}
		else if(dt_name == "T_PROC_NAME_DETAIL_" && obj == "LIMIT_TAG"){
			o_text = o_text.replace("1","年为基础");
			o_text = o_text.replace("2","月为基础");
			o_text = o_text.replace("3","日为基础");
		}
		$("#pagecontent").find("[name=\"T_INTERFACE_"+proc_id+"\"]").eq(0).find("[name='"+dt_name+obj+"']").find("span").text(o_text);
	}
}

$(document).ready(function () {
	init_page();
});

$("#export_word").click(function (event) {
	$("#pagecontent").wordExport(s_title+"数据接口文档"+dateFmt("yyyyMMddhh", new Date()) );
});

String.prototype.replaceAll = function(s1,s2){
　　return this.replace(new RegExp(s1,"gm"),s2);
}
